package com.demo.OtherTest;

import cn.hutool.core.map.MapUtil;
import org.apache.commons.collections.MapUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class DistanceTest {
    public static void main(String[] args) {
//        System.out.println(distance_format(getDistance(120.067952,30.324773,120.077570,30.357052,1,2)));

        Double le = 1230.2;
        Map<String,Object> map = new HashMap<>();
        map.put("a",1320.36);
        map.put("b","fg");
        map.put("c","");

        String a1 = MapUtils.getString(map, "a");
        Double ll = (Double) map.get("a");
        String a = String.valueOf(ll);
        String b = (String)map.get("b");
        String c = (String)map.get("c");
        String d = (MapUtils.getString(map,"d"));
        System.out.println(map.get(a));
        System.out.println(map.get(b));
        System.out.println(map.get(c));
        System.out.println(map.get(d));
    }

    /**
     * 计算两点地理坐标之间的距离
     * @param  Double longitude1 起点经度
     * @param  Double latitude1  起点纬度
     * @param  Double longitude2 终点经度
     * @param  Double latitude2  终点纬度
     * @param  int     unit       单位 1:米 2:公里
     * @param  int     decimal    精度 保留小数位数
     * @return Decimal
     */
    private static double getDistance(Double longitude1, Double latitude1, Double longitude2, Double latitude2, int unit, int decimal){

        Double EARTH_RADIUS = 6370.996; // 地球半径系数
        Double PI = 3.1415926;

        Double radLat1 = latitude1 * PI / 180.0;
        Double radLat2 = latitude2 * PI / 180.0;

        Double radLng1 = longitude1 * PI / 180.0;
        Double radLng2 = longitude2 * PI /180.0;

        Double a =  radLat1 -  radLat2;
        Double b =  radLng1 -  radLng2;

        Double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b/2),2)));
        distance = distance * EARTH_RADIUS * 1000;

        if(unit==2){
            distance = distance / 1000;
        }
        BigDecimal bg = new BigDecimal(distance);
        double d3 = bg.setScale(decimal, BigDecimal.ROUND_HALF_UP).doubleValue();
        return d3;
    }

    private static String distance_format(double distance) {
        if (distance > 50000) {
            return "";
        } else if (distance > 1000) {
            BigDecimal bg = new BigDecimal(distance / 1000);
            double d3 = bg.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            return d3 + "km";
        }
        return Math.round(distance) + "m";
    }
}
